package com.leyou.item.web;

import com.leyou.common.dto.*;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpecParamService specParamService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }


    @GetMapping("spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuPage(
            @RequestParam(value = "saleable" ,required = false)Boolean saleable,
            @RequestParam(value = "brandid", required = false)Integer bid,
            @RequestParam(value = "categoryid", required = false)Integer cid,
            @RequestParam(value = "id", required = false)Integer sid,
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows
            ){

        return ResponseEntity.ok(spuService.querySpuPage(page, rows, saleable, cid, bid, sid));
    }

    /**
     * 新增商品
     */
    @PostMapping("spu")
    @Transactional
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 商品上下架
     */

    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Integer id,
                                               @RequestParam("saleable")Boolean saleable){
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询商品详情
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<List<SpuDetailDTO>> querSpuDeatilById(@RequestParam("id")Integer id){
        return ResponseEntity.ok().body(spuDetailService.querSpuDeatilById(id));
    }


    /**
     * 根据id查询spu及sku、spuDetail等
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuSkuSpuDetail(@PathVariable("id")Integer id){
      return spuService.querySpuSkuSpuDetail(id);
    }


    /**
     * 更新商品
     */
    @PutMapping("spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        try {
            spuService.updateMyGoods(spuDTO);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

    }

    /**
     * 根据id查询商品
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Integer id){
        return  ResponseEntity.ok().body(spuService.getBySpuId(id));
    }

    /**
     * 根据spuID查询sku的集合
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Integer id){
        return ResponseEntity.ok().body(skuService.querySkuBySpuId(id));
    }


    /**
     * 根据spuId查询spu的所有规格参数值
     * /spec/value
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamBySpuId(@RequestParam("id") Integer id,
                                                                    @RequestParam("searching")Boolean searching){
        return ResponseEntity.ok().body(specParamService.querySpecParamBySpuId(id, searching));
    }

    /**
     * 根据id批量查询sku
     * @param ids skuId的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    List<SkuDTO> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return skuService.querySkuByIds(ids);
    }

}